package com.mcglib.flac
{
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	import com.mcglib.flac.scrollBar.IScrollContainer;
	import com.mcglib.flac.scrollBar.ScrollScope;
	
	
	
	/**
	 * 滚动条代理类
	 */ 
	public class ScrollBar extends EventDispatcher 
	{
		/** 总是显示滚动条 **/
		public static const SCROLLBAR_ALWAYS:int=1;
		/**在需要的时候显示滚动条**/
		public static const SCROLLBAR_AS_NEEDED:int=2;
		/**从不显示滚动条**/
		public static const SCROLLBAR_NEVER:int=3;
		
		/**滚动条滑块的最小高度**/
		private static const SCROLLBAR_MIN_HEIGHT:int=50;
		/**滚动条滑块一次移动的最小高度**/
		private static const SCROLLBAR_MOVE_DELTA:int=10;
		
		private var _contaienr:IScrollContainer;
		private var _vScrollUI:MovieClip;
		private var _hScrollUI:MovieClip;
		
		private var _vScrollScope:ScrollScope;
		private var _hScrollScope:ScrollScope;
		
		private var _isDragVScrollBar:Boolean=false;
		private var _isDragHScrollBar:Boolean=false;
		
		private var _vsbPolicy:int;
		private var _hsbPolicy:int;
		
		private var _vSlider:MovieClip;
		
		private var _vTrack:DisplayObject;
		
		private var _vSliderMcButton:McButton;
		
		/**
		 * @param vScrollUI		垂直滚动条UI
		 * @param vsbPolicy		垂直滚动条显示策略
		 * @param hScrollUI		水平滚动条UI
		 * @param hsbPolicy		水平滚动条显示策略
		 */
		public function ScrollBar(vScrollUI:MovieClip,vsbPolicy:int=SCROLLBAR_NEVER,hScrollUI:MovieClip=null,hsbPolicy:int=SCROLLBAR_NEVER)
		{
			super();
			_vScrollUI=vScrollUI;
			_hScrollUI=hScrollUI;
				
			_vsbPolicy=vsbPolicy;
			_hsbPolicy=hsbPolicy;
			checkScrollUI();
		}

		/**
		 * 受控制的滚动容器
		 */ 
		public function get contaienr():IScrollContainer
		{
			return _contaienr;
		}

		/**
		 * @private
		 */ 
		public function set contaienr(value:IScrollContainer):void
		{
			_contaienr = value;
			_contaienr.underControl(this);
			
			if(_vsbPolicy != SCROLLBAR_NEVER){
				configVScrollBarEvent();
				_vScrollScope=new ScrollScope(0,0,0);
				_vScrollScope.start=_vTrack.y;
				_vScrollScope.end=_vTrack.y+_vTrack.height;
				_vScrollScope.height=_vTrack.height;//_vScrollUI.height-_vScrollUI.btnVScrollUp.height-_vScrollUI.btnVScrollDown.height;
				setVScrollBarShow(false);
			}
			if(_hsbPolicy != SCROLLBAR_NEVER){
				configHScrollBarEvent();
				_hScrollScope=new ScrollScope(0,0,0);
				_hScrollScope.start=_hScrollUI.btnHScrollLeft.x+_hScrollUI.btnHScrollLeft.width;
				_hScrollScope.end=_hScrollUI.btnHScrollRight.x;
				_hScrollScope.height=_hScrollUI.width-_hScrollUI.btnHScrollLeft.width-_hScrollUI.btnHScrollRight.width;
				setHScrollBarShow(false);
			}
			
		}

		/**
		 * 由IScrollContainer调度,更新垂直滚动条UI
		 */ 
		public function notifyUpdateVScroll():void{
			if(_vsbPolicy != SCROLLBAR_NEVER){
				if(contaienr.viewScope.height>=contaienr.maxScope.height){
//					_vScrollUI.btnVScrollBar.height=_vScrollUI.height-_vScrollUI.btnVScrollUp.height-_vScrollUI.btnVScrollDown.height;
					updateBarHeight(_vScrollUI.height-_vScrollUI.btnVScrollUp.height-_vScrollUI.btnVScrollDown.height);
					setVScrollBarShow(false);
					scrollToTop();
				}else if(contaienr.viewScope.height<contaienr.maxScope.height){
					var scrollHeight:Number=(contaienr.maxScope.height-contaienr.viewScope.height)/5;//按照5：1的比例来滚动,计算结果为滚动条滑块可移动的区域
					var scrollBarHeight:Number=_vScrollScope.height-scrollHeight;//滚动条滑块的高度
					if(scrollBarHeight<SCROLLBAR_MIN_HEIGHT){
						scrollBarHeight=SCROLLBAR_MIN_HEIGHT;
					}
//					_vScrollUI.btnVScrollBar.height=scrollBarHeight;
					updateBarHeight(scrollBarHeight);
					setVScrollBarShow(true);
					if(_vSlider.y+_vSlider.height>_vScrollScope.end)
						scrollToBottom();	
					updateVerticalViewScope();
				}
			}
		}
		
		private function updateBarHeight(h:Number):void{
			if(h>200){
				h=200;
			}
			_vSlider.height=h;
			/*_vSlider.mcBottom.y=_vSlider.mcBar.y+h;
			_vSlider.mcIcon.y=_vSlider.mcBar.y+(h-_vSlider.mcIcon.height)/2;*/
		}
		
		/**
		 * 由IScrollContainer调度,更新水平滚动条UI
		 */ 
		public function notifyUpdateHScroll():void{
			if(_hsbPolicy != SCROLLBAR_NEVER){
				if(contaienr.viewScope.width>=contaienr.maxScope.width){
					_hScrollUI.btnHScrollBar.width=_hScrollUI.width-_hScrollUI.btnHScrollLeft.width-_hScrollUI.btnHScrollRight.width;
					setHScrollBarShow(false);
				}else if(contaienr.viewScope.width<contaienr.maxScope.width){
					var scrollWidth:Number=(contaienr.maxScope.width-contaienr.viewScope.width)/5;//按照5：1的比例来滚动,计算结果为滚动条滑块可移动的区域
					var scrollBarWidth:Number=_hScrollUI.width-scrollWidth;//滚动条滑块的高度
					if(scrollBarWidth<SCROLLBAR_MIN_HEIGHT){
						scrollBarWidth=SCROLLBAR_MIN_HEIGHT;
					}
					_hScrollUI.btnHScrollBar.width=scrollBarWidth;
					setHScrollBarShow(true);
				}
				scrollToLeft();
				updateHorizontalViewScope();
			}
		}
		
		
		/**
		 * 鼠标中部滑轮滚动时的处理
		 */	
		private function onVScrollBarWheelHandler(event:MouseEvent):void
		{
			if(_vScrollUI.visible && _vSlider.visible){
				if(event.delta>0){
					//向上滑动
					onVScrollUpClickHandler();
				}else{
					//向下滑动
					onVScrollDownClickHandler();
				}
			}
		}
		
		/**
		 * 	拖动时，鼠标移动的事件处理
		 */
		private function onScrollBarMoveHandler(event:MouseEvent):void
		{
			if(_isDragVScrollBar){
				_vSlider.y=_vScrollUI.mouseY-_vSlider.height/2;
				if(_vSlider.y<_vScrollScope.start){
					_vSlider.y=_vScrollScope.start;
				}
				if(_vSlider.y+_vSlider.height>_vScrollScope.end){
					_vSlider.y=_vScrollScope.end-_vSlider.height;
				}
				updateVerticalViewScope();
			}else if(_isDragHScrollBar){
				_hScrollUI.btnHScrollBar.x=_hScrollUI.mouseX-_hScrollUI.btnHScrollBar.width/2;
				if(_hScrollUI.btnHScrollBar.x<_hScrollScope.start){
					_hScrollUI.btnHScrollBar.x=_hScrollScope.start;
				}
				if(_hScrollUI.btnHScrollBar.x+_hScrollUI.btnHScrollBar.width>_hScrollScope.end){
					_hScrollUI.btnHScrollBar.x=_hScrollScope.end-_hScrollUI.btnHScrollBar.width;
				}
				updateHorizontalViewScope();
			}
		}
		
		/**
		 *	拖动时，鼠标抬起的事件处理
		 */
		private function onStopDragHandler(event:MouseEvent):void
		{
			if(_isDragVScrollBar){
				_isDragVScrollBar=false;
				_vScrollUI.stage.removeEventListener(MouseEvent.MOUSE_MOVE,onScrollBarMoveHandler);
				_vScrollUI.stage.removeEventListener(MouseEvent.MOUSE_UP,onStopDragHandler);
			}
			if(_isDragHScrollBar){
				_isDragVScrollBar=false;
				_hScrollUI.stage.removeEventListener(MouseEvent.MOUSE_MOVE,onScrollBarMoveHandler);
				_hScrollUI.stage.removeEventListener(MouseEvent.MOUSE_UP,onStopDragHandler);
			}
		}
		
		/**
		 *	垂直滚动条拖动的事件处理
		 */
		private function onVStartDragHandler(event:MouseEvent):void
		{
			if(_vScrollUI.visible && _vSlider.visible){
				_isDragVScrollBar=true;
				var mc:MovieClip=event.currentTarget as MovieClip;
				configDragEevent(mc);
			}
		}
		
		/**
		 *	水平滚动条拖动的事件处理
		 */
		private function onHStartDragHandler(event:MouseEvent):void
		{
			_isDragHScrollBar=true;
			var mc:MovieClip=event.currentTarget as MovieClip;
			configDragEevent(mc);
		}
		
		/**
		 *	配置当前拖动的mc的移动事件
		 */
		private function configDragEevent(mc:MovieClip):void{
			mc.stage.addEventListener(MouseEvent.MOUSE_MOVE,onScrollBarMoveHandler);
			mc.stage.addEventListener(MouseEvent.MOUSE_UP,onStopDragHandler);
		}
		
		/**
		 *滚动条向下的按钮被点击的处理 
		 */
		private function onVScrollDownClickHandler(event:MouseEvent=null):void
		{
			if(_vSlider.y+_vSlider.height==_vScrollScope.end){
				return;
			}
			
			if(_vSlider.y+_vSlider.height+SCROLLBAR_MOVE_DELTA>_vScrollScope.end){
				_vSlider.y=_vScrollScope.end-_vSlider.height;
			}else{
				_vSlider.y+=SCROLLBAR_MOVE_DELTA;
			}
			updateVerticalViewScope();
		}
		
		/**
		 *滚动条向上的按钮被点击的处理 
		 */
		private function onVScrollUpClickHandler(event:MouseEvent=null):void
		{
			if(_vScrollScope.start==_vSlider.y){
				return;
			}
			if(_vSlider.y-SCROLLBAR_MOVE_DELTA<_vScrollScope.start){
				_vSlider.y=_vScrollScope.start;
			}else{
				_vSlider.y-=SCROLLBAR_MOVE_DELTA;
			}
			updateVerticalViewScope();
		}
		
		/**
		 * 水平滚动条向右按钮被点击的处理
		 */
		private function onHScrollRightClickHandler(event:MouseEvent):void
		{
			if(_hScrollUI.btnHScrollBar.x+_hScrollUI.btnHScrollBar.width==_hScrollUI.btnHScrollRight.x){
				return;
			}
			
			if(_hScrollUI.btnHScrollBar.x+_hScrollUI.btnHScrollBar.width+SCROLLBAR_MOVE_DELTA>_hScrollUI.btnHScrollRight.x){
				_hScrollUI.btnHScrollBar.x=_hScrollUI.btnHScrollRight.x-_hScrollUI.btnHScrollBar.width;
			}else{
				_hScrollUI.btnHScrollBar.x+=SCROLLBAR_MOVE_DELTA;
			}
			updateHorizontalViewScope();
		}
		
		/**
		 * 水平滚动条向左按钮被点击的处理
		 */
		private function onHScrollLeftClickHandler(event:MouseEvent):void
		{
			if(_hScrollUI.btnHScrollBar.x==_hScrollUI.btnHScrollLeft.x+_hScrollUI.btnHScrollLeft.width){
				return;
			}
			
			if(_hScrollUI.btnHScrollBar.x-SCROLLBAR_MOVE_DELTA<_hScrollUI.btnHScrollLeft.x+_hScrollUI.btnHScrollLeft.width){
				_hScrollUI.btnHScrollBar.x=_hScrollUI.btnHScrollLeft.x+_hScrollUI.btnHScrollLeft.width;
			}else{
				_hScrollUI.btnHScrollBar.x-=SCROLLBAR_MOVE_DELTA;
			}
			updateHorizontalViewScope();
		}
		
		private function updateHorizontalViewScope():void{
			var listHeight:Number=_contaienr.maxScope.width-_contaienr.viewScope.width;
			var offsetX:int=listHeight*((_hScrollUI.btnHScrollBar.x-_hScrollScope.start)/(_hScrollScope.height-_hScrollUI.btnHScrollBar.width));
			contaienr.updateHorizontalViewScope(offsetX);
		}
		
		private function updateVerticalViewScope():void{
			var listHeight:Number=_contaienr.maxScope.height- _contaienr.viewScope.height;
			var offsetY:int=listHeight*((_vSlider.y-_vScrollScope.start)/(_vScrollScope.height-_vSlider.height));
			contaienr.updateVerticalViewScope(offsetY);
		}
		
		/**
		 *垂直移动滚动条滑块到最底部 
		 */
		public function scrollToBottom():void{
			_vSlider.y=_vScrollScope.end-_vSlider.height;//_vScrollUI.btnVScrollDown.y-_vSlider.height;
		}
		
		/**
		 *垂直移动滚动条滑块到最顶部 
		 */
		public function scrollToTop():void{
			_vSlider.y=_vScrollScope.start;//_vScrollUI.btnVScrollUp.y+_vScrollUI.btnVScrollUp.height;
		}
		
		/**
		 *水平移动滚动条滑块到最右边
		 */
		public function scrollToRight():void{
			_hScrollUI.btnHScrollBar.x=_hScrollUI.btnHScrollRight.x-_hScrollUI.btnHScrollBar.width;
		}
		
		/**
		 *水平移动滚动条滑块到最顶部 
		 */
		public function scrollToLeft():void{
			_hScrollUI.btnHScrollBar.x=_hScrollUI.btnHScrollLeft.x+_hScrollUI.btnHScrollLeft.width;
		}
		
		/**
		 * 配置垂直滚动条事件
		 */
		private function configVScrollBarEvent():void{
			_vScrollUI.btnVScrollUp.addEventListener(MouseEvent.CLICK,onVScrollUpClickHandler);
			_vScrollUI.btnVScrollDown.addEventListener(MouseEvent.CLICK,onVScrollDownClickHandler);
			
			_vScrollUI.addEventListener(MouseEvent.MOUSE_WHEEL,onVScrollBarWheelHandler);
			_vScrollUI.addEventListener(MouseEvent.MOUSE_DOWN,onVStartDragHandler);
			_vScrollUI.addEventListener(MouseEvent.MOUSE_UP,onStopDragHandler);
		}
		
		/**
		 * 配置水平滚动条事件
		 */	
		private function configHScrollBarEvent():void{
			_hScrollUI.btnHScrollLeft.addEventListener(MouseEvent.CLICK,onHScrollLeftClickHandler);
			_hScrollUI.btnHScrollRight.addEventListener(MouseEvent.CLICK,onHScrollRightClickHandler);
			
			_hScrollUI.addEventListener(MouseEvent.MOUSE_DOWN,onHStartDragHandler);
			_hScrollUI.addEventListener(MouseEvent.MOUSE_UP,onStopDragHandler);
		}
		
		private function setVScrollBarShow(isNeedShow:Boolean):void{
			if(_vsbPolicy==SCROLLBAR_AS_NEEDED){
				//在需要显示的时候才显示滚动条,所以控制整个滚动条是否显示
				_vScrollUI.visible=isNeedShow;
			}else{
				//总是显示滚动条，所有只需要控制中间滑块是否显示
				_vSlider.visible=isNeedShow;
			}
		}
		
		private function setHScrollBarShow(isNeedShow:Boolean):void{
			if(_hsbPolicy==SCROLLBAR_AS_NEEDED){
				//在需要显示的时候才显示滚动条,所以控制整个滚动条是否显示
				_hScrollUI.visible=isNeedShow;
			}else{
				//总是显示滚动条，所有只需要控制中间滑块是否显示
				_hScrollUI.btnHScrollBar.visible=isNeedShow;
			}
		}
		/**
		 * 如果需要显示滚动条，则检查滚动条UI命名是否符合规则
		 */
		private function checkScrollUI():void{
			
			_vSlider = _vScrollUI.slider;
			_vSliderMcButton = new McButton(_vSlider);
			
			_vTrack = _vScrollUI.track;
			
			if(_vsbPolicy != SCROLLBAR_NEVER){
				
			}
			
			if(_hsbPolicy != SCROLLBAR_NEVER){
				
			}
			
		}
		
	}
}